<?php # 5.2.4

class ClassMatch
{
	private
		$a, $b
	;
	
	public
	function __construct()
	{
		echo("Se incia la pelea\n");
	}
	
	public
	function __destruct()
	{
		echo("\n"); # Fix some terminals
	}
	
	public
	function start($nameA, $nameB)
	{
		echo("*** $nameA vs $nameB ***\n");

		$winner = $this->fight(
			$armyA = new $nameA(),
			$armyB = new $nameB()
		);
		
		if($winner !== false)
		{
			echo("Ganador ejercito {$winner->name}\nSobrevivientes:\n");
			foreach($winner->soldiers as $soldier)
				echo("\t{$soldier->id}({$soldier->life}, {$soldier->resistance}, {$soldier->stamina})\n");
			
		}
		
		$a = $armyA->getStat();
		$b = $armyB->getStat();
		
		echo("{$armyA->name}: $a puntos - {$armyB->name}: $b\n");
	}
	
	public
	function fight(CMArmy $a, CMArmy $b)
	{
		$armies = array($a, $b);
		shuffle($armies);
	
		$rounds = rand(10, 30);
		
		for($i=1, $j=0; $i < $rounds; $j++)
		{
			if($j == 0)
			{
				echo("\nRonda $i de $rounds...\n");
			}
			
			$attacker = $armies[CM_ATTACK];
			$defend = $armies[CM_DEFEND];
			
			if($attacker->attack($defend) === false)
				return $attacker;
			
			if($defend->idDefited())
				return $attacker;
			
			$armies = array_reverse( $armies );
			
			if($j == 2)
			{
				++$i;
				$j = 0;
			}
		}
		return false;
	}
}

?>